阿里开源规则引擎QLExpress 您所在的位置:网站首页 阿里云规则引擎 服务器 阿里开源规则引擎QLExpress

阿里开源规则引擎QLExpress

2023-07-14 00:33| 来源: 网络整理| 查看: 265

规则引擎简介

规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策。接受数据输入,解释业务规则,并根据业务规则做出业务决策。

规则引擎

常用的规则引擎目前主要有Drools、Aviator、Easyrule、QLExpress,如下表格是对这些规则引擎的分析对比: 在这里插入图片描述 结合本人实际项目,我们的项目业务属性是电商,对性能要求比较高,因此发现阿里开源的QLExpress更适合现实的诉求,下面的章节将以电商促销活动的例子介绍QLExpress如何落地实操到业务代码中的

电商促销活动示例

比如我们的购物车现在有两个商品,对应的商品SKU分别为11120781和11120782,商品11120781参与的单品促销优惠活动,11120782参与的是单品促销打折活动,假设这两个商品来自同一个店家,店家给的运费优惠是满100减50关于两个活动规则的介绍如下:

单品促销优惠 该活动是在商品价格基础上优惠xxx元,比如商品原价为150,促销优惠50元,则优惠后的金额为150-50=100

单品促销打折 该活动是在商品价格基础上进行折扣,比如商品原价为150,促销打折5折,则优惠后的金额为150*0.5=75

第一步 引入依赖包

如下图所示,引入QLExpress的包

com.alibaba QLExpress 3.2.0 编写针对某一类活动规则的业务接口

此接口主要处理同一类型规则的逻辑,以上述的单品促销为例,代码示例如下:

/** * 查询商品促销活动匹配单品促销规则计算后的优惠金额 * * @param promotionList 参与的促销活动 * @param productPrice 商品价格 * @return */ private List querySinglePromotionPrice(List promotionList, double productPrice) { List nicePrices = Lists.newArrayList(); DefaultContext context = new DefaultContext(); ExpressRunner expressRunner = new ExpressRunner(); promotionList = promotionList.stream().filter(r -> "singlePromotion".equals(r.getPromotionType())).collect(Collectors.toList()); for (Promotion promotion : promotionList) { //查询促销活动对应的规则模版配置信息 RuleTemplate template = queryRuleTemplate(promotion.getPromotionType()); String express = template.getExpress(); context.put("price", productPrice); context.put("promotionType", promotion.getPromotionWay()); context.put("promotionValue", Double.parseDouble(promotion.getPromotionValue())); System.out.println("参与促销活动【" + promotion.getPromotionName() + "】优惠前的商品金额:" + productPrice); Object execute = null; try { execute = expressRunner.execute(express, context, null, true, true); } catch (Exception e) { System.out.println("计算规则[" + template.getTemplateName() + "]时发生异常,原因:" + e.getMessage()); } System.out.println("优惠后的金额:" + execute); nicePrices.add((Double) execute); } return nicePrices; }

涉及到的运费计算,代码示例如下:

private double queryMinShippingFree(List promotionList,double productPrice,double freeShipping ){ DefaultContext context = new DefaultContext(); double minShipping = 0.0; List niceShipping = Lists.newArrayList(); ExpressRunner expressRunner = new ExpressRunner(); promotionList = promotionList.stream().filter(r -> "freeShipping".equals(r.getPromotionType())).collect(Collectors.toList()); for (Promotion promotion : promotionList) { RuleTemplate template = queryRuleTemplate(promotion.getPromotionType()); String express = template.getExpress(); String condition = promotion.getConditionConfig(); context.put("price", productPrice); context.put("amount", Double.parseDouble(JSONObject.parseObject(condition).getString("amountCondition"))); context.put("shippingPrice",freeShipping); context.put("promotionValue", Double.parseDouble(JSONObject.parseObject(condition).getString("promotionAmount"))); System.out.println("参与促销活动【" + promotion.getPromotionName() + "】优惠前的运费:" + freeShipping); Object execute = null; try { execute = expressRunner.execute(express, context, null, true, true); } catch (Exception e) { System.out.println("计算规则[" + template.getTemplateName() + "]时发生异常,原因:" + e.getMessage()); } niceShipping.add((Double) execute); } if(niceShipping.isEmpty()){ return minShipping; } minShipping = Collections.min(niceShipping); System.out.println("优惠后的运费:" + minShipping); return minShipping; }

封装以上两个方法,输出购物车中的最终优惠金额,代码示例如下:

/** * 计算最优促销活动价格 * * @param cart 购物车信息 * @param shippingFee 运费 * @return 计算出最优价格 * @throws Exception */ @Override public double queryMinPromotionPrice(String cart,double shippingFee) { JSONArray cartArray = JSONObject.parseArray(cart); double sum = 0.0,sumNicePrice = 0.0,sumNiceShipping = 0.0; for(int i=0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有